package cc.rengu.igas.channel.alipay.core.service.trans;

import cc.rengu.igas.channel.alipay.common.constant.AlipayConfigConstant;
import cc.rengu.igas.channel.alipay.common.constant.AlipayParamConstant;
import cc.rengu.igas.channel.alipay.common.constant.AlipayTreeNodeConstant;
import cc.rengu.igas.channel.alipay.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.alipay.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.alipay.core.model.request.CupsAlipayNoticeRequest;
import cc.rengu.igas.channel.alipay.core.service.base.NoticeService;
import cc.rengu.igas.channel.alipay.facade.base.BaseRequest;
import cc.rengu.igas.channel.alipay.facade.base.Header;
import cc.rengu.jradp.service.dubbo.Dubbo;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.BizMsgInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.BizMsgInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.BizMsgInfo;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.lang.reflect.Method;

/**
 * 活动报名审核消息
 */
public class ActivityResultNotice extends NoticeService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        CupsAlipayNoticeRequest cupsAlipayNoticeRequest = new CupsAlipayNoticeRequest();
        ConvertUtil.convertOutput(cupsAlipayNoticeRequest);
        BaseRequest baseRequest = new BaseRequest();
        Header header = new Header();
        ConvertUtil.convertOutput(header);
        header.setTransDate(DateUtil.getCurrentDate());
        header.setTransDate(DateUtil.getCurrentTime());
        header.setTxnNum("xxxxxx");
        header.setChanlId(AlipayParamConstant.TUNL_ID_ALIP);
        header.setTraceNo(AlipayParamConstant.TUNL_ID_ALIP + DateUtil.getCurrentDate() + DateUtil.getCurrentTime());
        baseRequest.setHeader(header);
        ConvertUtil.convertInput(baseRequest);
        return cupsAlipayNoticeRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysSrcId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SRC_SYS_ID);
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(instId, sysSrcId + AlipayConfigConstant.UPAL_CONF_NODE,
                AlipayConfigConstant.BEAN_NAME);
        if (sysParam == null) {
            rglog.error("未配置异步通知服务，请配置");
            throw new BizException(RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespCode(), RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespDesc());
        }
        String beanName = sysParam.getParamValue();
        sysParam = sysParamService.getSysParamInfo(instId, sysSrcId + AlipayConfigConstant.UPAL_CONF_NODE,
                AlipayConfigConstant.METHOD_NAME);
        if (sysParam == null) {
            rglog.error("未配置异步通知服务，请配置");
            throw new BizException(RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespCode(), RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespDesc());
        }
        String methodName = sysParam.getParamValue();
        sysParam = sysParamService.getSysParamInfo(instId, sysSrcId + AlipayConfigConstant.UPAL_CONF_NODE,
                AlipayConfigConstant.REQUEST_CLASS_NAME);
        if (sysParam == null) {
            rglog.error("未配置异步通知服务，请配置");
            throw new BizException(RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespCode(), RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespDesc());
        }
        Class<?> clasz = Class.forName(sysParam.getParamValue());
        Object dubboRequest = clasz.newInstance();
        ConvertUtil.convertOutput(dubboRequest);
        Object interfaceService = Dubbo.getBean(beanName);
        Method method = interfaceService.getClass().getDeclaredMethod(methodName, clasz);
        Object dubboResponse = method.invoke(interfaceService, dubboRequest);
        rglog.debug("dubbo异步调用结果：{}", dubboResponse);
        return null;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {

    }

    private void registerBizMsgInfo(RgLogger rglog, String bizMsg) {
        try {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            if (bizMsg.length() > 8000) {
                rglog.error("存储转发报文超过8K，暂不登记到存储转发表:<{}>", bizMsg);
                return;
            }
            BizMsgInfo bizMsgInfo = new BizMsgInfo();
            BizMsgInfoMapper bizMsgInfoMapper = new BizMsgInfoMapperImpl();
            BaseRequest baseRequest = new BaseRequest();
            ConvertUtil.convertOutput(baseRequest);
            bizMsgInfo.setInstId(baseRequest.getHeader().getInstId());
            bizMsgInfo.setChannelId(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.MSG_SRC_ID));
            bizMsgInfo.setTransSeqNum(baseRequest.getHeader().getTraceNo());
            bizMsgInfo.setTxnDate(baseRequest.getHeader().getTransDate());
            bizMsgInfo.setTxnTime(baseRequest.getHeader().getTransTime());
            bizMsgInfo.setDstTxnNum(baseRequest.getHeader().getTxnNum());
            bizMsgInfo.setCreateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
            if (bizMsg.length() < 4000) {
                bizMsgInfo.setBizReqMsg(bizMsg);
            } else {
                bizMsgInfo.setBizReqMsg(bizMsg.substring(0, 4000));
                bizMsgInfo.setBizReqExpandMsg(bizMsg.substring(4000));
            }
            int iReturnCode = bizMsgInfoMapper.insertBizMsgInfo(bizMsgInfo);
            if (0 != iReturnCode) {
                return;
            }
            xmlTreeUtil.setXmlTreeObjectValue(AlipayTreeNodeConstant.ALIP_BIZ_MSG_INFO, bizMsgInfo);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("登记业务报文信息表异常:<{}>", byteArrayOutputStream.toString());
        }
    }
}
